package wec_wallet.ebo.com.activity.main;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import java.util.ArrayList;

import bassproject.ebo.com.ebobass.activity.BaseFragment;
import bassproject.ebo.com.ebobass.activity.MyBassAppCompatActivity;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.PermissionUtils;
import bassproject.ebo.com.ebobass.view.NoScrollViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import devlight.io.library.ntb.NavigationTabBar;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.intercept.PhoneListenService;
import wec_wallet.ebo.com.util.PermissionsChecker;
import wec_wallet.ebo.com.widget.BlockNavigationTabBar;


public class MainActivity extends MyBassAppCompatActivity implements BlockNavigationTabBar.OnTabClickListener {


    @BindView(R.id.viewpager)
    NoScrollViewPager viewPager;
    @BindView(R.id.tabbar)
    BlockNavigationTabBar navigationTabBar;

    FragmentPageAdapter adapter;


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initUI();
        registerPhoneStateListener();
    }

    private void initUI() {
        viewPager = findViewById(R.id.viewpager);
        adapter = new FragmentPageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
//        viewPager.setNoAnimation();

        navigationTabBar = findViewById(R.id.tabbar);
        navigationTabBar.setTabClickListener(this);
        final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_home_default), Color.RED)
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_home_selected))
                        .title(getString(R.string.tab_home))
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_assets_default), Color.TRANSPARENT)
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_assets_seleted))
                        .title(getString(R.string.tab_assets))
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_social_default), Color.TRANSPARENT)
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_social_selected))
                        .title(getString(R.string.tab_social))
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_mine_default), Color.TRANSPARENT)
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_mine_selected))
                        .title(getString(R.string.tab_mine))
                        .build()
        );


        navigationTabBar.setModels(models);
        navigationTabBar.setViewPager(viewPager, 0);
        navigationTabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                navigationTabBar.getModels().get(position).hideBadge();
            }

            @Override
            public void onPageScrollStateChanged(final int state) {

            }
        });


    }

    @Override
    public boolean onPre(int position) {

        if ( position == 2) {
            show_Toast(getString(R.string.no_open));
            return false;
        }
        return true;
    }

    private void registerPhoneStateListener() {
        String[] primissions = {Manifest.permission.READ_PHONE_STATE};
        PermissionUtils.getInstance().checkPermission(this, primissions, new PermissionUtils.OnPermissionChangedListener() {
            @Override
            public void onGranted() {
                Intent intent = new Intent(MainActivity.this, PhoneListenService.class);
                intent.setAction(PhoneListenService.ACTION_REGISTER_LISTENER);
                bindService(intent, conn, Context.BIND_AUTO_CREATE);
            }
            @Override
            public void onDenied() {
                IToast.show(getResources().getString(bassproject.ebo.com.ebobass.R.string.permission_failed));
            }
        });


    }

    private PhoneListenService service = null;
    private boolean isBound = false;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            isBound = true;
            service = ((PhoneListenService.MyBinder) binder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound = false;
        }
    };


    public void gotoPage(int index) {
        navigationTabBar.setModelIndex(index,true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        BaseFragment fragment = adapter.getItem(viewPager.getCurrentItem());
        if(fragment instanceof AbsImageFragment){
            fragment.onActivityResult(requestCode,resultCode,data);
        }
    }

    //Bandage Sample
//            navigationTabBar.postDelayed(new Runnable() {
//        @Override
//        public void run() {
//            for (int i = 0; i < navigationTabBar.getModels().size(); i++) {
//                final NavigationTabBar.Model model = navigationTabBar.getModels().get(i);
//                navigationTabBar.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        model.showBadge();
//                    }
//                }, i * 100);
//            }
//        }
//    }, 500);


    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


}
